package hc.bre.runner

@groovy.transform.CompileStatic
class ClassLoaderRunner extends RuleRunnerSupportEx {
	@groovy.transform.CompileDynamic
	public Object run(Object namedArgsMap) {
		Map map = namedArgsMap as Map;
		if(map==null)map= [:];

		Class clazz = buildRuleClass();
		Closure rule = clazz.getField('rule').get(clazz) as Closure;

		//		rule =  rule.clone() as Closure;
		//		rule.delegate = map;
		//		def ret =  rule.call(map) ;
		//		(map as Map).result = ret;
		//		return map;

		map.result = map.apply(rule);
		return map;
	}
	
	public Class buildRuleClass() {
		GroovyClassLoader classLoader =
				new GroovyClassLoader(Thread.currentThread().getContextClassLoader(),
				configuration(ruledir));

		Class clazz = null;
		try
		{
			clazz = classLoader.loadClass(classFullName(),true,false,false);
		}
		catch(Exception ex)
		{
			RuleRunnerSupport._log.error("failed to load the class of.\n{}",rpath)
			throw ex;
		}
		return clazz
	}
	
}
